package com.example.androidclass;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

//判断两字符串是否相等用str1 == str2可能会有错误，因为str1和str2可能是两个不同的对象
//如 String str1 = "abc"; String str2 = "abc" 这时str1 = str2, 他们都指向"abc"这个对象
//但 String str1 = new String("abc"); String str2 = new String("abc") 这时str1 != str2， 因为这是两个不同的对象

//在 onCreate 里面声明了 Button btn0 = (Button) findViewById(R.id.btn0) 但是在 onCreate 外面不可见，
//因为 onCreate 是 protected 类型, 所以先在 onCreate 外声明 btn0, 再在 onCreate 里面实例化

//这次是真的怎么玩儿都玩不坏的计算器，检测多个0，多个小数点，除0等问题。
//未修复问题：运算时浮点数精度问题，删除键还未做

public class Calculator extends AppCompatActivity implements View.OnClickListener {

    String first = "";
    String second = "";
    String operator = "";
    String result = "";
    String tag = "";
    int dot = 0;
    Float temp;
    private Button btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn_add,btn_sub,btn_multi,btn_div,btn_dot,btn_del,btn_equal,btn;
    private TextView textView_result,textView_temp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calculator);
        Intent intent = getIntent();
        String data = intent.getStringExtra("extra_data");
        Log.d("Click", "111");

        btn0 = findViewById(R.id.btn0);
        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        btn3 = findViewById(R.id.btn3);
        btn4 = findViewById(R.id.btn4);
        btn5 = findViewById(R.id.btn5);
        btn6 = findViewById(R.id.btn6);
        btn7 = findViewById(R.id.btn7);
        btn8 = findViewById(R.id.btn8);
        btn9 = findViewById(R.id.btn9);
        btn_add = findViewById(R.id.btn_add);
        btn_sub = findViewById(R.id.btn_sub);
        btn_div = findViewById(R.id.btn_div);
        btn_dot = findViewById(R.id.btn_dot);
        btn_del = findViewById(R.id.btn_del);
        btn_equal = findViewById(R.id.btn_equal);
        btn_multi = findViewById(R.id.btn_multi);
        textView_result = (TextView) findViewById(R.id.textView_result);
        textView_temp = (TextView) findViewById(R.id.textView_temp);

        btn0.setOnClickListener(this);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
        btn8.setOnClickListener(this);
        btn9.setOnClickListener(this);
        btn_add.setOnClickListener(this);
        btn_sub.setOnClickListener(this);
        btn_div.setOnClickListener(this);
        btn_dot.setOnClickListener(this);
        btn_del.setOnClickListener(this);
        btn_equal.setOnClickListener(this);
        btn_multi.setOnClickListener(this);

        textView_result.setText(data);
        first = data;
    }

    @Override
    public void onBackPressed() {
        Intent intent_result = new Intent();
        intent_result.putExtra("data_return", first);
        setResult(RESULT_OK, intent_result);
        super.onBackPressed();
    }

    public void show() {
        result = first + operator + second;
        textView_result.setText(result);
        if (second != "") {
            if (Float.valueOf(second).equals(Float.valueOf("0.0")) && operator.equals("/")) {
                textView_temp.setText("输入错误❎");
            }
            else {
                switch (operator) {
                    case "+":
                        temp = Float.valueOf(first) + Float.valueOf(second);break;
                    case "-":
                        temp = Float.valueOf(first) - Float.valueOf(second);break;
                    case "*":
                        temp = Float.valueOf(first) * Float.valueOf(second);break;
                    case "/":
                        temp = Float.valueOf(first) / Float.valueOf(second);break;
                }
                String drop_temp = String.valueOf(temp);
                drop_temp = drop_zero(drop_temp);
                textView_temp.setText(drop_temp);
            }
        }
    }

    public void show_result() {
        switch (operator) {
//            这里用float类型是为了防止整数相除为小数，int会取整
            case "+":
                temp = Float.valueOf(first) + Float.valueOf(second);break;
            case "-":
                temp = Float.valueOf(first) - Float.valueOf(second);break;
            case "*":
                temp = Float.valueOf(first) * Float.valueOf(second);break;
            case "/":
                temp = Float.valueOf(first) / Float.valueOf(second);break;
        }
        first = String.valueOf(temp);
        first = drop_zero(first);
        textView_result.setText(first);
        textView_temp.setText("");
        second = "";
    }

//    因为temp为浮点数,所以不管是不是小数,后面都会有小数点,如2.0,这里判断一下是不是整数
    public String drop_zero(String drop) {
        if (!drop.equals("0")) {
            String temp_to_string = String.valueOf(drop);
            int dot_index = temp_to_string.indexOf(".");
            String after_dot = temp_to_string.substring(dot_index + 1);
            String before_dot = temp_to_string.substring(0,dot_index);
            if (Integer.valueOf(after_dot) == 0){
                drop = before_dot;
            }
        }
        return drop;
    }

//    以下两个函数判断是否能继续添加小数点，能添加设置dot = 0，不能添加设置dot = 1
    public void drop_dot() {
        if (dot == 1) {
            if (!operator.equals("") && second.equals("")) {
                dot = 0;
            }
        }
    }

    public void drop_dot_result() {
        if (!operator.equals("")) {
            dot = 0;
        }
        else if (first.indexOf(".") > 0 && operator.equals("")) {
            dot = 1;
        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn1:
            case R.id.btn2:
            case R.id.btn3:
            case R.id.btn4:
            case R.id.btn5:
            case R.id.btn6:
            case R.id.btn7:
            case R.id.btn8:
            case R.id.btn9:
            case R.id.btn0:
            case R.id.btn_dot:
                btn = (Button) findViewById(view.getId());
                String num = String.valueOf(btn.getText());
//                还没有输入运算符的情况，即第一个数还没有输入完
                if (operator == "") {
                    Log.d("Click", "here");
//                    防止输入的数字出现0002这种前面多个0的情况，若有0继续输入0，则只显示一个0，若输入其他数字，则显示该数字
                    if (first.equals("0")) {
                        if (num.equals("0")) {
                            break;
                        }
                        else if (!num.equals(".")) {
                            first = num;
                        }
                    }
                    if (num.equals(".")) {
                        if (dot == 0 && first.equals("")) {
                                first = "0.";
                                dot = 1;
                        }
                        else if (dot == 1) {
                            break;
                        }
                        else {
                            first = first + num;
                            dot = 1;
                        }
                    }
                    else {
                        first = first + num;
                    }
                }
                else {
                    if (second.equals("0")) {
                        if (num.equals("0")){
                            break;
                        }
                        else if (!num.equals(".")){
                            second = num;
                        }
                    }
                    if (num.equals(".")) {
                        if (dot == 0 && second.equals("")) {
                            second = "0.";
                            dot = 1;
                        }
                        else if (dot == 1) {
                            break;
                        }
                        else {
                            second = second + num;
                            dot = 1;
                        }
                    }
                    else {
                        second = second + num;
                    }
                }
                show();
                break;

            case R.id.btn_add:
            case R.id.btn_sub:
            case R.id.btn_multi:
            case R.id.btn_div:
                btn = findViewById(view.getId());
                tag = String.valueOf(btn.getTag());
                if (first == "") {
                    break;
                }
                if (first != "" && second == "") {
                    operator = tag;
                    show();
                    drop_dot();
                    break;
                }
                if (first != "" && second != "") {
                    if (second.equals("0") && operator.equals("/")){
                        textView_result.setText("输入错误");
                        textView_temp.setText("");
                        first = "";
                        second = "";
                        operator = "";
                        break;
                    }
                    show_result();
                    operator = tag;
                    show();
                    textView_temp.setText("");
                    drop_dot_result();
                    break;
                }
                break;

            case R.id.btn_equal:
//                只有在第一第二个数都有值才运行
                if (first != "" && second != "") {
                    if (second.equals("0") && operator.equals("/")){
                        textView_result.setText("输入错误");
                        textView_temp.setText("");
                        first = "";
                        second = "";
                        operator = "";
                        break;
                    }
                    else {
                        show_result();
                        operator = "";
                        drop_dot_result();
                    }
                }
        }
    }
}
